<?php
class C{

	//_left_html_cache
	function left($gid){
		$power	= include( B_DIR . '/_res/power/' . $gid . '.php');
		$left_B	= include( B_DIR . '/_res/tree/left_B.php');
		$str = '<div id="col"><div id="_col">'
			.self::__left($left_B, $power).
				'</div></div>';
		file_put_contents(B_DIR . '/_res/power/' . $gid . '.txt', $str);
	}

	function __left(&$L, &$power){
		$str = '';
		if(is_array($L)){
			foreach($L as $k=>&$v){
				if( empty($power[0][$k]) ){
					continue;
				}
				if($v['isParent']){
					$str .= '<a class="a0"><b>' . $v['name'] . '</b></a>';
					//d   class="D"
					$str .= '<div class="v0">' . self::__left($v['child'], $power) . '</div>';
				}else{
					$url = '?mod=' . $v['mod']; 
					if( $v['fun'] ){
						$url	.= '&fun=' . $v['fun'];
					}
					if( $v['act'] ){
						$url	.= '&act=' . $v['act'];
					}
					if( $v['tail'] ){
						$url	.= '&' . $v['tail'] . '=' . $v['detail'];
					}
					//class="a"
					$str	.= '<a href="' . $url . '"><b>' . $v['name'] . '</b></a>';
				}
			}
			unset($v);
		}
		return $str;		
	}


	// change_cache_tree 
	function tree($tab, $order, $file, $sel='*'){
		$L = E::$DB->ary($tab, '1 ' . $order, $sel);
		$B = array();
		$A = array();
		foreach($L as $k=>$v){
			$id = $v['id'];
			$pid = $v['pid'];
			if( isset($A[$id]) ) {
				throw new Exception( '循环错误 ,' . $id);
				die;
			}
			if( $v['isParent'] ){
				$v['child'] = array();
			}
			$A[$id] = $v;
			if( $pid ){		
				$A[$pid]['child'][$id] = &$A[$id]; 
			}else{
				$B[$id] = &$A[$id];
			}
		}
		hash::save($A, B_DIR . '/_res/tree/' . $file . '_A.php' );
		hash::save($B, B_DIR . '/_res/tree/' . $file . '_B.php' );
	}

	
	function leftTitle(){
		$left_A	= include( B_DIR . '/_res/tree/left_A.php');
		$title	= array();
		C::leftPower($left_A, $title, '[\'');
		$json	= array();
		foreach($title as $k => $v){
			array_push($json, '\'' . $k . '\':' . $v . '\']');
		}
		//die( 'var _AdminTitle = {' . join(',', $json) . '};' );
		file_put_contents( B_DIR . '/_res/js/title.js', 'var _AdminTitle = {' . join(',', $json) . '};');
	}

	function leftPower(&$left_A, &$T, $pre){
		foreach($left_A as $v){
			if( is_array($v['child']) ) {
				self::leftPower($v['child'], $T, $pre . $v['name'] . '\',\'');
			}
			$key	= $v['mod'] . '.' . $v['fun'];
			$val	= $pre . $v['name'];
			if( empty($T[$key])   || strlen($val) > strlen($T[$key]) ){
				$T[$key]	=  $val;
			}
		}
	}

	//table_cache_all
	function table(){
		$L	= E::$DB->all('_table_list');
		foreach($L as $v){
			self::__table($v['id']);
		}
	}

	function __table($id){
		$T = E::$DB->one('_table_list', 'id=' . $id);
		$F = E::$DB->ary('_table_field', 'tid=' . $T['id'] . ' ORDER BY gid ASC, orderId DESC , id ASC','*','fieldKey');
		foreach($F as $k=>$f)	{
			if(
				$f['bindId'] && 
				in_array($f['elementType'], array('select', 'checkbox', 'radio')) 
			){
				$l	= E::$DB->all('_super_param', 'pid=' . $f['bindId'] . ' order by orderId DESC , id ASC');
				$f['child']	= array();
				foreach($l as $_k=>$_v){
					$_k_	= $_v['paramValue'] !== '' ? $_v['paramValue'] : $_v['id'];
					$f['child'][$_k_] = $_v['paramName'];
				}
				
			}
			$F[$k] = $f;
		}
		$T['field'] = $F;
		$tmp		= E::$DB->one('_column', 'isUseTab=1 AND tab=' . $id, 'id');
		if( !$tmp ){
			$T['ColId']	= 0;
		}else{
			$T['ColId']	= $tmp['id'];
		}
		unset($T['ShowInfo']);
		unset($T['isGroup']);
		file_put_contents( B_DIR . '/_res/form/' . $id . '.php', '<?php $T =  ' . var_export($T, 1) . ';?'.'>');
	}

	//param_cache
	function param($pid = 1 ){
		$l	= self::__param($pid);
		hash::save($l, B_DIR . '/_res/tree/PA.php');
		$L	= array();
		self::__param_col($L, $l);
		hash::save($L, B_DIR . '/_res/tree/PB.php');
		self::tree('_super_param', 'ORDER BY deep ASC, pid ASC, orderId DESC, id ASC' , 'param');
	}

	function &__param($pid){
		$l	= E::$DB->ary('_super_param', 'pid=' . $pid . ' ORDER BY orderId DESC, id ASC', '*', 'id');
		$L	= array();
		foreach($l as $k=>$v){
			if($v['isParent']){
				$a	= self::__param($v['id']);
				$L[$k]	= array($v['paramName'], $v['pid'], $a);
			}else{
				$L[$k]	= array($v['paramName'], $v['pid']);
			}
		}
		return $L;
	}

	function __param_col(&$L, $l){
		if(is_array($l)){
			foreach($l as $k=>$v){
				$L[$k]	= $v;
				if(is_array($v[2])){
					self::__param_col($L, $v[2]);
				}
			}
		}
	}

}
?>